
'use strict'

const { Sequelize, DBPool } = require('./mysqldb')

const KnightSession = function() {
  return DBPool.define(
    'session',
    {
      token: {
        type: Sequelize.CHAR,
        allowNull: false,
        defaultValue: '',
        length: 10
      },
      uid: {
        type: Sequelize.BIGINT,
        allowNull: false,
        defaultValue: 0
      }
    },
    {
      tableName: 'session',
      timestamps: true,
      indexes: [{
        name: 'UQE_session_token',
        type: 'UNIQUE',
        unique: true,
        using: 'BTREE',
        fields: ['token']
      }]
    }
  )
}

const KnightUser = function() {
  return DBPool.define(
    'user',
    {
      id: {
        type: Sequelize.BIGINT,
        allowNull: false,
        primaryKey: true,
        autoIncrement: true
      },
      parent_uid: {
        type: Sequelize.BIGINT,
        allowNull: false,
        defaultValue: 0
      },
      username: {
        type: Sequelize.STRING,
        allowNull: false,
        defaultValue: ''
      },
      password: {
        type: Sequelize.STRING,
        allowNull: false,
        defaultValue: ''
      },
      salt: {
        type: Sequelize.STRING,
        allowNull: false,
        defaultValue: '',
        length: 10
      },
      introduction: {
        type: Sequelize.STRING,
        allowNull: false,
        defaultValue: ''
      },
      avatar: {
        type: Sequelize.STRING,
        allowNull: false,
        defaultValue: 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif'
      },
      phone: {
        type: Sequelize.STRING,
        allowNull: false,
        defaultValue: ''
      },
      role_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        defaultValue: 0
      },
      enabled: {
        type: Sequelize.BOOLEAN,
        allowNull: false,
        defaultValue: true
      }
    },
    {
      tableName: 'user',
      timestamps: true,
      indexes: [{
        name: 'UQE_user_username',
        type: 'UNIQUE',
        unique: true,
        using: 'BTREE',
        fields: ['username']
      }]
    }
  )
}

const TableKnightSession = new KnightSession()
const TableKnightUser = new KnightUser()

module.exports = {
  TableKnightSession,
  TableKnightUser
}
